package com.gxa.zhonglong.controller;

import com.gxa.zhonglong.common.JsonResult;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.pojo.Discount;
import com.gxa.zhonglong.service.DiscountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @auther JJuventus
 * @date 2021/10/26 10:09
 */

@RestController
@RequestMapping("/discount")
@Api(value = "客户折扣")
@CrossOrigin
@Transactional(rollbackFor = Throwable.class)
public class DiscountController {

    @Autowired
    private DiscountService discountService = null;

    @RequestMapping("/getList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "want",value = "模糊查询的内容,无内容则为分页查询",dataType = "string"),
            @ApiImplicitParam(name = "page",value = "当前页",required = true,dataType = "int"),
            @ApiImplicitParam(name = "size",value = "每页记录数",required = true,dataType = "int")
    })
    @ApiOperation(value = "查询客户资料的方法")
    public Map select(@RequestParam("want") String want,@RequestParam("page") Integer page,@RequestParam("size") Integer size){
        List<Discount> discounts = discountService.select(want, page, size);
        List<Discount> all = discountService.selectAll(want);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.put("total",all.size());
        jsonResult.setData(discounts);
        return jsonResult.getDataMap();
    }

    @RequestMapping("/add")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "discount",value = "添加的折扣",required = true,paramType = "body")
    )
    @ApiOperation(value = "添加客户资料的方法")
    public Map add(@RequestBody Discount discount) throws BusinessException {
        discountService.add(discount);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @RequestMapping("/delete")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "discountId",value = "需要删除的折扣的id",required = true,dataType = "long")
    )
    @ApiOperation(value = "删除客户资料的方法")
    public Map delete(@RequestParam("discountId")Long discountId) throws BusinessException {
        discountService.delete(discountId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @RequestMapping("/update")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "discount",value = "修改的折扣",required = true,paramType = "body")
    )
    @ApiOperation(value = "修改客户资料的方法")
    public Map update(@RequestBody Discount discount) throws BusinessException {
        discountService.update(discount);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }
}
